import Vue from 'vue'
import Router from 'vue-router'
import login from '@/pages/login'
import index from '@/pages/index'
import info from '@/pages/info'
import user from '@/pages/user'
import delivery from '@/pages/delivery'
import store from '../store'
import address from '@/pages/address'
import creatOrder from '@/pages/creatOrder'
import payState from '@/pages/payState'
import orderList from '@/pages/orderList'
import deliverGoods from '@/pages/deliverGoods'
import safetyInfo from '@/pages/safetyInfo'
import evaluate from '@/pages/evaluate'
import statistics from '@/pages/statistics'
import protocol from '@/pages/protocol'
import resetPwd from '@/pages/resetPwd'
import classIndex from '@/pages/classIndex'
import addressList from '@/pages/addressList'
import orderInfo from '../pages/orderInfo'
import userAgreement from '../pages/userAgreement'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path:'/userAgreement',
      name:'userAgreement',
      component:userAgreement,
      meta:{
        title:"用户协议",
        footer:false,
        back:true,
        isLogin:false
      }
    },
    {
      path: '/',
      name: 'login',
      component: login,
      meta:{
        title:"大学食代",
        footer:false,
        back:false,
        isLogin:false
      }
    },{
      path: '/index',
      name: 'index',
      component: index,
      meta:{
        title:"首页",
        footer:true,
        back:false,
        isLogin:true
      }
    },
    {
      path: '/info',
      name: 'info',
      component: info,
      meta:{
        title:"点餐",
        footer:false,
        back:true,
        isLogin:true
      }
    },
    {
      path:'/user',
      name:user,
      component:user,
      meta:{
        title:"个人中心",
        footer:true,
        back:false,
        isLogin:true
      }
    },
    {
      path:'/delivery',
      name:'delivery',
      component:delivery,
      meta:{
        title:"注册配送员",
        footer:false,
        back:true,
        isLogin:true
      }
    },
    {
      path:'/address',
      name:'address',
      component:address,
      meta:{
        title:"收货地址修改",
        footer:false,
        back:true,
        isLogin:true
      }
    },
    {
      path:'/creatOrder',
      name:'creatOrder',
      component:creatOrder,
      meta:{
        title:'提交订单',
        footer:false,
        back:true,
        isLogin:true
      }
    },
    {
      path:'/payState',
      name:'payState',
      component:payState,
      meta:{
        title:"支付",
        footer:false,
        back:false,
        isLogin:true
      }
    },
    {
      path:'/orderList',
      namae:'orderList',
      component:orderList,
      meta:{
        title:'订单列表',
        footer:true,
        back:false,
        isLogin:true
      }
    },
    {
      path:'/deliverGoods',
      name:'deliverGoods',
      component:deliverGoods,
      meta:{
        title:"配送订单",
        footer:false,
        back:true,
        isLogin:true
      }
    },
    {
      path:'/safetyInfo',
      name:'safetyInfo',
      component:safetyInfo,
      meta:{
        title:"食品安全档案",
        footer:false,
        back:true,
        isLogin:true
      }
    },
    {
      path:'/evaluate',
      name:'evaluate',
      component:evaluate,
      meta:{
        title:"评价",
        footer:false,
        back:true,
        isLogin:true
      }
    },
    {
      path:'/statistics',
      name:'statistics',
      component:statistics,
      meta:{
        title:"配送统计",
        footer:false,
        back:true,
        isLogin:true
      }
    },
    {
      path:'/protocol',
      name:'protocol',
      component:protocol,
      meta:{
        title:"骑手协议",
        footer:false,
        back:true,
        isLogin:true
      }
    },
    {
      path:'/resetPwd',
      name:'resetPwd',
      component:resetPwd,
      meta:{
        title:'忘记密码?',
        footer:false,
        back:true,
        isLogin:false
      }
    },
    {
      path:'/classIndex',
      name:'classIndex',
      component:classIndex,
      meta:{
        title:'分类查询',
        footer:false,
        back:true,
        isLogin:true
      }
    },
    {
      path:'/addressList',
      name:'addressList',
      component:addressList,
      meta:{
        title:'收货地址管理',
        footer:false,
        back:true,
        isLogin:true
      }
    },{
      path:'/orderInfo',
      name:'orderInfo',
      component:orderInfo,
      meta:{
        title:"订单详情",
        footer:false,
        back:true,
        isLogin:true
      }
    }
  ]
});
router.beforeEach((to, from, next) => {
  // to and from are both route objects. must call `next`.
  if (!window.initUrl) {
    window.initUrl = location.href.split('#')[0]
  }
  if(to.meta.isLogin){
    if(store.getters.token){
      next()
    }else{
      next({path:"/"})
    }
  }else{
    if(to.path == '/' && store.getters.token){
      next({path:"/index"})
    }else{
      next();
    }
  }
})
export default router;